//
//  ViewController.m
//  TableviewNestCollectionview
//
//  Created by 岚海网络 on 2018/12/19.
//  Copyright © 2018年 ljm. All rights reserved.
//

#import "ViewController.h"
#import "TableViewCell.h"
#import "CustomCollectionView.h"
#import "CollectionViewCell.h"
#import "YBNetworking.h"
#import <MJExtension/MJExtension.h>
#import "NewSearchModel.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface ViewController () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic , strong) NSMutableArray * CommentArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self initUI];
    [self loadWithNetWork];
    
}


- (void)initUI
{
    CGFloat safeTop = 0;
    if (@available(iOS 11.0, *)) {
        safeTop =  [UIApplication sharedApplication].keyWindow.safeAreaInsets.top;
    } else {
        // Fallback on earlier versions
    }
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + safeTop, [UIScreen mainScreen].bounds.size.width, 600) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 170;
    // 注册cell
//    [self.tableView registerClass:[CustTableCell class] forCellReuseIdentifier:@"tableIdentifier"];
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableView的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableIdentifier"];
    TableViewCell *cell = [TableViewCell setupWithTableview:tableView];
    [cell.CollectionView reloadData];
    return cell;
}

// 将UITableViewCell显示之前，给对应的collection设置响应的属性
- (void)tableView:(UITableView *)tableView willDisplayCell:(TableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.CollectionView.delegate = self;
    cell.CollectionView.dataSource = self;
//    [cell.CollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collectionIdentifier"];
    [cell.CollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"collectionIdentifier"];
    // 这里还需要给cell.collection设置是属于哪一行的rowIndex
    cell.CollectionView.rowIndex = indexPath;

}




//// 指定UITableViewCell的高度
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 145.0;
//}

#pragma mark - UICollectionView的代理方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.CommentArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionIdentifier" forIndexPath:indexPath];
    CommentModel *model = self.CommentArray[indexPath.row];
    [cell.ImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.thumb]] placeholderImage:[UIImage imageNamed:@"bg"]];
    cell.TapImageWithBlock = ^(UITapGestureRecognizer * _Nonnull gesture) {
        NSLog(@"你点击了第%ld个item",indexPath.row);
    };
    return cell;
}

- (void)collectionView:(CustomCollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"行数：%d，collectio位置：%d ", (int)collectionView.rowIndex.row, (int)indexPath.row);
}


#pragma mark - loadWithNetWork
- (void) loadWithNetWork
{
    NSString *url = [NSString stringWithFormat:@"http://fy.demo.haidao.la/api/public/?service=Search.GetSalonList"];
    NSDictionary *dic = @{@"type":@"2"};
    
    [YBNetworking postWithUrl:url Dic:dic Suc:^(NSDictionary *data, NSString *code, NSString *msg) {
        NSLog(@"data:%@",data);
        NSLog(@"code:%@",code);
        NSLog(@"msg:%@",msg);
        
        if (data) {
            NSArray *info = data[@"info"];
            NSLog(@"data:%@",info);
            if (info && [info isKindOfClass:NSArray.class] && info.count > 0) {
                self.CommentArray = [CommentModel mj_objectArrayWithKeyValuesArray:info];
            }   else    {
                self.CommentArray = [[NSMutableArray alloc] init];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
        
        if ([msg containsString:@"null"]) {
            
        }
        
    } Fail:^(id fail) {
        NSLog(@"拍客推荐接口出错:%@",fail);
    }];
}

- (NSMutableArray *)CommentArray
{
    if (!_CommentArray) {
        _CommentArray = [NSMutableArray array];
    }
    return _CommentArray;
}

@end
